
// 公共文件下载
export function downloadFile(res, fName, type) {
  var blob = new Blob([res], { type }); // 指定格式为vnd.ms-excel
  var downloadElement = document.createElement('a');
  var href = window.URL.createObjectURL(blob); // 创建下载的链接
  downloadElement.href = href;
  downloadElement.download = fName; // 下载后文件名
  document.body.appendChild(downloadElement);
  downloadElement.click(); // 点击下载
  document.body.removeChild(downloadElement); // 下载完成移除元素
  window.URL.revokeObjectURL(href); // 释放掉blob对象
}

// 获取操作系统和浏览器
export function getBrowserInfo() {
  var agent = navigator.userAgent.toLowerCase();
  var arr = [];
  var system = agent
    .split(' ')[1]
    .split(' ')[0]
    .split('(')[1];
  arr.push(system);
  var regStr_edge = /edge\/[\d.]+/gi;
  var regStr_ie = /trident\/[\d.]+/gi;
  var regStr_ff = /firefox\/[\d.]+/gi;
  var regStr_chrome = /chrome\/[\d.]+/gi;
  var regStr_saf = /safari\/[\d.]+/gi;
  var regStr_opera = /opr\/[\d.]+/gi;
  // IE
  if (agent.indexOf('trident') > 0) {
    arr.push(agent.match(regStr_ie)[0].split('/')[0]);
    arr.push(agent.match(regStr_ie)[0].split('/')[1]);
    return arr;
  }
  // Edge
  if (agent.indexOf('edge') > 0) {
    arr.push(agent.match(regStr_edge)[0].split('/')[0]);
    arr.push(agent.match(regStr_edge)[0].split('/')[1]);
    return arr;
  }
  // firefox
  if (agent.indexOf('firefox') > 0) {
    arr.push(agent.match(regStr_ff)[0].split('/')[0]);
    arr.push(agent.match(regStr_ff)[0].split('/')[1]);
    return arr;
  }
  // Opera
  if (agent.indexOf('opr') > 0) {
    arr.push(agent.match(regStr_opera)[0].split('/')[0]);
    arr.push(agent.match(regStr_opera)[0].split('/')[1]);
    return arr;
  }
  // Safari
  if (agent.indexOf('safari') > 0 && agent.indexOf('chrome') < 0) {
    arr.push(agent.match(regStr_saf)[0].split('/')[0]);
    arr.push(agent.match(regStr_saf)[0].split('/')[1]);
    return arr;
  }
  // Chrome
  if (agent.indexOf('chrome') > 0) {
    arr.push(agent.match(regStr_chrome)[0].split('/')[0]);
    arr.push(agent.match(regStr_chrome)[0].split('/')[1]);
    return arr;
  } else {
    arr.push('请更换主流浏览器，例如chrome,firefox,opera,safari,IE,Edge!');
    return arr;
  }
}

// 获取下个月
export function getCurrentNextMonth() {
  var datetime = new Date();
  var year = datetime.getFullYear();
  // var month = datetime.getMonth() + 1 < 10 ? '0' + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
  var month = datetime.getMonth() + 2;
  if (month === 13) {
    year++;
    month = '01';
  } else {
    month = month < 10 ? '0' + month : month;
  }
  const tips = year + '-' + month;
  return tips;
}

// 获取当前的年月日时分秒
export function getCurrentTime() {
  var datetime = new Date();
  var year = datetime.getFullYear();
  var month = datetime.getMonth() + 1 < 10 ? '0' + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
  var date = datetime.getDate() < 10 ? '0' + datetime.getDate() : datetime.getDate();
  var hour = datetime.getHours() < 10 ? '0' + datetime.getHours() : datetime.getHours(); // 获取系统时，
  var minutes = datetime.getMinutes() < 10 ? '0' + datetime.getMinutes() : datetime.getMinutes(); // 分
  var seconds = datetime.getSeconds() < 10 ? '0' + datetime.getSeconds() : datetime.getSeconds(); // 秒

  const tips = year + '-' + month + '-' + date + ' ' + hour + ':' + minutes + ':' + seconds;
  return tips;
}

// 数字补零方法
export function returnFloat(value1) {
  let value = Math.round(parseFloat(value1) * 100) / 100;
  var s = value.toString().split('.');
  if (s.length === 1) {
    value = value.toString() + '.00';
    return value;
  }
  if (s.length > 1) {
    if (s[1].length < 2) {
      value = value.toString() + '0';
    }
    return value;
  }
}

// 结束时间不能小于开始时间
export function validTime(startTime, endTime) {
  var arr1 = startTime.split('-');
  var arr2 = endTime.split('-');
  var date1 = new Date(parseInt(arr1[0]), parseInt(arr1[1]) - 1, parseInt(arr1[2]), 0, 0, 0);
  var date2 = new Date(parseInt(arr2[0]), parseInt(arr2[1]) - 1, parseInt(arr2[2]), 0, 0, 0);
  if (date1.getTime() > date2.getTime()) {
    return false;
  } else {
    return true;
  }
}

// 生成唯一值
export function uuid() {
  // 获取唯一值
  var s = [];
  var hexDigits = '0123456789abcdef';
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = '4'; // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = '-';
  var uuid = s.join('');
  return uuid;
}

/**
 * 获取session 中的值
 *
 * @export
 * @param {string} name
 * @returns {string}
 */

export function getLocalStorage(name) {
  return localStorage.getItem(name);
}

/**
 *
 * 设置session 的key 和value
 * @export
 * @param {string} name
 * @param {(string | any)} value
 */
export function setLocalStorage(name, value) {
  localStorage.setItem(name, value);
}

/**
 *
 *清空 session 的值
 * @export
 */
export function clearStorage() {
  localStorage.clear();
}

/**
 number：传入的千分位展示字符串。必选
thousand：千分位分隔符。必选
isSymbol：千分位展示字符串末尾的标识。可选，传入后，传入的千分位字符串长度减去isSymbol的长度
 */

export function toNumber(number, thousand, isSymbol) {
  if (isSymbol) {
    number = number.substring(0, number.len);
  }
  number = number.split(thousand).join('');
  return number;
}

// 保存的时候转换
// export function changeValueKey(item, value) {
//   const { selectViewOptions, columnSelectOptions, componentType } = item;
//   // 需要转换的组件
//   const changeData = ['select', 'radio'].includes(componentType);
//   if (changeData && columnSelectOptions && columnSelectOptions.length && selectViewOptions && Object.keys(selectViewOptions).length && value) {
//     const { renderKey, renderValue } = selectViewOptions;
//     const findValueObj = columnSelectOptions.find(item => item[renderValue] === value);
//     const findValue = findValueObj ? findValueObj[renderKey] : value;
//     return findValue;
//   } else {
//     return value;
//   }
// }
